package indi.jp.project.core.service.impl;

import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Menu;
import indi.jp.project.core.entity.User;
import indi.jp.project.core.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    private BaseDao baseDao;

    public UserServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Map<String, Object> queryGrid(User entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<User> list = baseDao.queryList("userMapper.queryList", entity);
        int count = baseDao.queryCount("userMapper.queryCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public List<Menu> queryUserMenu(String username) throws SQLException {
        if("admin".equals(username)){
            return baseDao.queryList("userMapper.queryAdminMenu", "");
        } else {
            return baseDao.queryList("userMapper.queryUserMenu", username);
        }
    }

    @Override
    public int lockUser(String id) throws SQLException {
        return baseDao.update("userMapper.lockUser", id);
    }

    @Override
    public int unlockUser(String id) throws SQLException {
        return baseDao.update("userMapper.unlockUser", id);
    }

    @Override
    public int save(User entity) throws SQLException {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        entity.setPassword(password);
        return baseDao.insert("userMapper.save", entity);
    }

    @Override
    public int edit(User entity) throws SQLException {
        return baseDao.update("userMapper.edit", entity);
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return baseDao.deleteById("userMapper.deleteById", id);
    }

    @Override
    public Map<String, Object> queryUserByRoles(Map<String, Object> params) throws SQLException{
        Map<String, Object> map = new HashMap<>();
        List<User> list = baseDao.queryList("userMapper.queryUserListByRoles", params);
        int count = baseDao.queryCount("userMapper.queryUserCountByRoles", params);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public void batchInsertUser(List<User> list) throws SQLException {
        baseDao.insert("userMapper.batchInsertUser", list);
    }

    @Override
    public JsonResult pwdModify(Map<String, String> map) throws SQLException {
        User entity = new User();
        entity.setUsername(map.get("username"));
        User user = (User) baseDao.findByObject("userMapper.loadUserByUsername", entity);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if(!encoder.matches(map.get("pwdBefore"), user.getPassword())){
            return JsonResult.failure("原密码不正确");
        } else {
            int effectRow = baseDao.update("userMapper.pwdModify", map);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("修改失败");
            }
        }
    }

    @Override
    public int pwdInit(User entity) throws SQLException {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        entity.setPassword(password);
        return baseDao.update("userMapper.pwdInit", entity);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        System.out.println(password);
    }
}
